package com.san.metadata.controller;

import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.san.base.controller.BaseController;
import com.san.common.annotation.Log;
import com.san.common.utils.PageUtils;
import com.san.common.utils.Query;
import com.san.common.utils.R;
import com.san.metadata.domain.ManageColumnsDO;
import com.san.metadata.service.DataManageService;
import com.san.metadata.service.ManageMetadataService;

/**
 * 管理元数据管理
 * 
 * @author hulp
 *
 */
@RequestMapping("/md/dataManage")
@Controller
public class DataManageController extends BaseController {

	private String PREFIX = "metadata/dataManage/";
	private static final Logger logger = LoggerFactory.getLogger(DataManageController.class);

	@Autowired
	DataManageService dataManageService;

	@Autowired
	ManageMetadataService manageMetadataService;

	@RequestMapping("")
	@RequiresPermissions("md:dataManage:view")
	String view(Model model) {
		return PREFIX + "view";
	}

	/**
	 * 管理元数据页面
	 * 
	 * @param model
	 * @param tableId 管理元数据表ID
	 * @return
	 */
	@RequestMapping("/table/{tableId}")
	@RequiresPermissions("md:dataManage:view")
	String table(Model model, @PathVariable("tableId") Long tableId) {
		model.addAttribute("tableId", tableId);
		return PREFIX + "table";
	}

	/**
	 * 获取所有字段信息
	 * 
	 * @param tableId 管理元数据表ID
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/allColumns/{tableId}")
	@RequiresPermissions("md:dataManage:view")
	R allColumns(@PathVariable("tableId") Long tableId) {
		List<ManageColumnsDO> columnsList = null;
		try {
			columnsList = manageMetadataService.allManageColumns(tableId);// 获取管理元表字段
		} catch (Exception e) {
			logger.error("获取管理元表字段出错", e);
		}
		return R.okReturnList(columnsList);
	}

	/**
	 * 获取管理元数据列表
	 * 
	 * @param params  查询参数
	 * @param tableId 管理元数据表ID
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/list/{tableId}")
	@RequiresPermissions("md:dataManage:view")
	PageUtils list(@RequestParam Map<String, Object> params, @PathVariable("tableId") Long tableId) {
		PageUtils pageUtils = null;
		try {
			// 查询列表数据
			Query query = new Query(params);
			List<Map<String, Object>> list = dataManageService.list(query, tableId);
			int total = dataManageService.count(query, tableId);
			pageUtils = new PageUtils(list, total);
		} catch (Exception e) {
			logger.error("获取业务元数据列表失败", e);
		}

		return pageUtils;
	}

	/**
	 * 编辑数据页面
	 * 
	 * @param model
	 * @param tableId    管理元数据表ID
	 * @param repeatMark 去重字段
	 * @return
	 */
	@RequestMapping("/edit/{tableId}/{repeatMark}")
	@RequiresPermissions("md:dataManage:view")
	String edit(Model model, @PathVariable("tableId") Long tableId, @PathVariable("repeatMark") String repeatMark) {
		model.addAttribute("tableId", tableId);
		model.addAttribute("repeatMark", repeatMark);
		return PREFIX + "edit";
	}

	/**
	 * 获取详细信息
	 * 
	 * @param tableId    管理元数据表ID
	 * @param repeatMark 去重字段
	 * @return
	 */
	@ResponseBody
	@RequiresPermissions("md:dataManage:view")
	@RequestMapping("/getDetail/{tableId}/{repeatMark}")
	PageUtils getDetail(@PathVariable("tableId") Long tableId, @PathVariable("repeatMark") String repeatMark) {
		List<ManageColumnsDO> columnsList = null;
		try {
			columnsList = manageMetadataService.allManageColumns(tableId);// 获取管理元表字段
		} catch (Exception e) {
			logger.error("获取管理元表字段出错", e);
		}

		List<Map<String, Object>> list = dataManageService.getByrepeatMark(tableId, repeatMark);
		Map<String, Object> resultValue = list.get(0);
		for (Entry<String, Object> entry : resultValue.entrySet()) {
			String mapKey = entry.getKey();
			for (ManageColumnsDO i : columnsList) {
				if (i.getColumnName().toLowerCase().equals(mapKey)) {
					i.setValue(entry.getValue());
				}
			}
		}
		return new PageUtils(columnsList, columnsList.size());
	}

	/**
	 * 删除数据
	 * 
	 * @param tableId    管理元数据表ID
	 * @param repeatMark 去重字段
	 * @return
	 */
	@ResponseBody
	@RequiresPermissions("md:dataManage:remove")
	@RequestMapping("/remove/{tableId}/{repeatMark}")
	@Log("删除管理元数据表数据")
	R remove(@PathVariable("tableId") Long tableId, @PathVariable("repeatMark") String repeatMark) {
		boolean flag = dataManageService.remove(tableId, repeatMark);
		if (flag) {
			return R.ok();
		} else {
			return R.error();
		}
	}

}